<?php

/**
 * $[Update.class] [ ATUALIZAÇÃO DE DADOS ]
 * Classe responsavel por atualizações generica no banco de dados!
 *
 *@copyright (c) 2015, Rafael Batista da Silva
 */
class Update extends Conn{

    private $Tabela;
    private $Dados;
    private $Termos;
    private $Places;
    Private $Result;

    /** @var PDOStatement */
    private $Update;

    /** @var PDO */
    private $Conn;

    public function ExeUpdate($Tabela, array $Dados, $Termos, $ParseString){
        $this->Tabela = (string) $Tabela;
        $this->Dados = $Dados;
        $this->Termos =  (string) $Termos;

        parse_str($ParseString, $this->Places);
        $this->getSyntax();
        $this->Execute();
    }

    public function getResult(){
        return $this->Result;
    }

    public function getRowCount(){
        return $this->Update->rowCount();
    }

    public function setPlaces($ParseString){
        parse_str($ParseString, $this->Places);
        $this->Execute();
    }

    /**
     * ****************************************
     * *********** PRIVATE METHODS ************
     * ****************************************
    */

    private  function Connect(){
        $this->Conn = parent::getConn();
        $this->Update = $this->Conn->prepare($this->Update);
    }

    private function getSyntax(){
        foreach ($this->Dados as $key => $key):
            $Places[] =  $key . ' = :' . $key;
        endforeach;

        $Places =  implode(', ', $Places);
        $this->Update = "UPDATE {$this->Tabela} SET {$Places} {$this->Termos}";
    }

    private function Execute(){
        $this->Connect();
        try {
            $this->Update->execute(array_merge($this->Dados, $this->Places));
            $this->Result = true;
        } catch (PDOException $e) {
            $this->Result = null;
            echo "Erro ao Ler arquivo.<br> <b>Erro:".$e->getMessage()."</b>";
        }
    }

}